import ajaxAsync from 'vue-xiaobu-utils'
// import { chacheAjax } from '@/utils/CacheAjaxUtils'
import CardInfoModel from '../models/CardInfoModel'
import CardPaymentModel from '../models/CardPaymentModel'
import {
  BUS_CARD_INSTANCE,
  STAFF_CARD_INSTANCE
} from '@/constants/CardInstance'

let apiModule = ''
let apiTradeModule = ''
// let apiModule = '/test2-city-api'
// let apiTradeModule = '/test2-trade-api'

/**
 * 获取失败行程次数
 */
async function getUnpaySuccessCount() {
  let body = await ajaxAsync(apiModule + '/posbill/unpaysuccess/count', {})
  return body.UNPAY_SUCCESS_COUNT
}
/**
 * 获取卡信息列表
 */
async function getCardList() {
  let body = await ajaxAsync(apiModule + '/vcard/info/list', {})
  let list = body.LIST
  let convertList = []
  list.map(item => {
    // if (item.CARD_INSTANCE_ID === STAFF_CARD_INSTANCE) {
    //   item.STATE = 0
    // }
    // 过滤未开通的员工卡
    if (item.CARD_INSTANCE_ID !== STAFF_CARD_INSTANCE || item.STATE !== 0) {
      convertList.push(new CardInfoModel(item))
    }
  })
  return convertList
}

/**
 * 获取卡请码相关配置，充值提示等
 *
 * @param {String} cardInstanceId 卡实例Id
 * @param {String} cardType 卡类型
 */
function getInstanceConfig() {
  let postData = {
    CARD_INSTANCE_ID: BUS_CARD_INSTANCE
    // CARD_TYPE: cardType
  }
  // let cacheKey = cardInstanceId + ';' + cardType + ';dsjakdsflkja'
  // 协议请求
  return ajaxAsync(apiModule + '/vcard/instance/config', postData)
}

/**
 * 获取指定卡类型的扣款顺序
 * @param {String} cardInstanceId 卡实例Id
 * @param {String} cardType 卡类型
 */
async function getInstancePayOrder(cardInstanceId, cardType) {
  let postData = {
    CARD_INSTANCE_ID: cardInstanceId,
    CARD_TYPE: cardType
  }
  let body = await ajaxAsync(apiModule + '/vcard/payment/order', postData)
  let resultList = []
  body.LIST.map(item => {
    resultList.push(new CardPaymentModel(item))
  })
  // if (resultList.length === 0) {
  //   resultList.push(new CardPaymentModel())
  // }
  return resultList
}

// 获取钱包余额
async function getWalletAmount() {
  let body = await ajaxAsync(apiTradeModule + '/wallet/balance/get', {})
  return parseFloat(body.BALANCE)
}

/**
 * 取消云卡申请
 * @param {String} applyId 申请Id
 */
function cancelCardApply(applyId) {
  return ajaxAsync('/vcard/apply/cancel', { APPLY_ID: applyId })
}

export {
  getUnpaySuccessCount,
  getCardList,
  getInstanceConfig,
  getInstancePayOrder,
  getWalletAmount,
  cancelCardApply
}
